//
//  HomeDietaryAdviceView.m
//  Ginix
//
//  Created by 冯志强 on 2025/3/26.
//

#import "HomeDietaryAdviceView.h"
#import "HomeIdeaDataCell.h"
#import "HomeStressCell.h"
#import "HomeSleepDataCell.h"
@interface HomeDietaryAdviceView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView * listtView;
@property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView);
@property (nonatomic,strong) HomeQuickCheckModel * infoModel;

@end

@implementation HomeDietaryAdviceView

-(instancetype)initWithFrame:(CGRect)frame{
    
    
    if (self = [super initWithFrame:frame]) {
        [self setSubViewLayout];
        [self requestData];
    }
    return self;
}

-(void)setSubViewLayout{
    
    self.backgroundColor = UIColor.clearColor;
    [self addSubview:self.listtView];
    [_listtView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).insets(UIEdgeInsetsMake(20 * FMScaleX, 0, 0, 0));
    }];
}

-(void)requestData{
    @WeakObj(self)
    [SPNetworkingIns POST:PORT(@"bh-center/api/invite/HealthDataAllLatestNew") params:@{} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
        @StrongObj(self)
        self.infoModel = [HomeQuickCheckModel yy_modelWithDictionary:responseObject[@"data"]];
        [self.listtView reloadData];
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 3;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        
    switch (indexPath.row) {
        case 0:
            return  200 * FMScaleX;
            break;
        case 1:
            return  230 * FMScaleX;
            break;
        case 2:
            return  self.infoModel.height;
            break;
        default:
            return 0;
            break;
    }}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
       
    if (indexPath.row == 0) {
        HomeStressCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeStressCell"];
        [cell setInfoModel:self.infoModel.normalYaLi];
        return cell;
    }
    if (indexPath.row == 1) {
        HomeSleepDataCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeSleepDataCell"];
        [cell setInfoModel:self.infoModel.sleepYaLi];
        return cell;
    }
    
    HomeIdeaDataCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeIdeaDataCell"];
    [cell setInfoModel:self.infoModel];
    return cell;
}

-(UITableView *)listtView{
    
    if (!_listtView) {
        _listtView = [[UITableView alloc]init];
        _listtView.delegate = self;
        _listtView.dataSource = self;
        _listtView.showsVerticalScrollIndicator = NO;
        _listtView.showsHorizontalScrollIndicator = NO;
        _listtView.backgroundColor = UIColor.clearColor;
        _listtView.bounces = NO;
        _listtView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_listtView registerClass:[HomeIdeaDataCell class] forCellReuseIdentifier:@"HomeIdeaDataCell"];
        [_listtView registerClass:[HomeStressCell class] forCellReuseIdentifier:@"HomeStressCell"];
        [_listtView registerClass:[HomeSleepDataCell class] forCellReuseIdentifier:@"HomeSleepDataCell"];

        

    }
    return _listtView;
}

- (UIScrollView *)listScrollView {
    return self.listtView;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    self.scrollCallback(scrollView);
}

- (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback {
    self.scrollCallback = callback;
}

- (UIView *)listView {
    return self;
}
@end
